package com.demo.boss.settings;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.demo.boss.entity.DicPayType;
import com.demo.boss.entity.DicPayWay;
import com.demo.boss.mapper.DicPayTypeMapper;
import com.demo.boss.mapper.DicPayWayMapper;
import com.demo.boss.models.vo.DicItemVo;
import com.demo.boss.tools.RedisHelper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目的相关配置类
 * CommandLineRunner : 是 springBoot中的接口，做项目的初始化擦操作，读取数据库配置数据
 * 读取配置文件，数据加密， 解密
 * <p>
 * 接口中有 run()方法 ，执行时机： 在spring的容器初始化完成后，自动执行
 */
@Configuration
public class BossConfiguration implements CommandLineRunner {
    @Resource
    private ApplicationContext applicationContext;
    // 支付类型集合
    public static Map<String, List<DicItemVo>> dicPayTypeMap = new HashMap<>();
    // 支付方式集合
    public static List<DicItemVo> dicPayWayDicList = new ArrayList<>();

    // 存储支付方式的Map
    public static Map<String, String> DIC_PAY_WAY_MAP = new HashMap<>();
    // 存储支付类型的map
    public static Map<String, String> DIC_PAY_TYPE_MAP = new HashMap<>();

    // 创建 RedisHelper
    @Bean
    public RedisHelper redisHelper(StringRedisTemplate stringRedisTemplate) {
        return new RedisHelper(stringRedisTemplate);
    }

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }

    @Override
    public void run(String... args) throws Exception {
        // 读取支付方式 dic_pay_way表的数据
        DicPayWayMapper dicPayWayMapper = applicationContext.getBean(DicPayWayMapper.class);  // mybatis类似操作
        List<DicPayWay> dicPayWayList = dicPayWayMapper.selectList(new QueryWrapper<DicPayWay>()
                .orderByAsc("pay_way_code"));
        // 将List<DicPayWay> --- > List<DicItemVo>
        dicPayWayDicList = dicPayWayList.stream().map((
                dicPayWay -> new DicItemVo(dicPayWay.getPayWayCode(), dicPayWay.getPayWayName()))).toList();
        // 转成 map 集合
        DIC_PAY_WAY_MAP = dicPayWayList.stream().collect(Collectors.toMap(DicPayWay::getPayWayCode,DicPayWay::getPayWayName));


        DicPayTypeMapper payTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        List<DicPayType> dicPayTypeList = payTypeMapper.selectList(new QueryWrapper<DicPayType>().orderByAsc("pay_type_code"));
        // List<DicPayType> dicPayTyeList --> Map<String,List<DicItemVo>>
        String payWayCode = "";

        for (DicPayType dicPayType : dicPayTypeList) {
            payWayCode = dicPayType.getPayWayCode();
            if (dicPayTypeMap.containsKey(payWayCode)) {
                dicPayTypeMap.get(payWayCode).add(new DicItemVo(dicPayType.getPayTypeCode(), dicPayType.getPayTypeName()));
            } else {
                List<DicItemVo> itemVosList = new ArrayList<>();
                itemVosList.add(new DicItemVo(dicPayType.getPayTypeCode(), dicPayType.getPayTypeName()));
                dicPayTypeMap.put(payWayCode, itemVosList);
            }
            // 添加至map集合
            DIC_PAY_TYPE_MAP.put(dicPayType.getPayTypeCode(),dicPayType.getPayTypeName());
        }
    }
}
